package com.qzxy.premiumhotelsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.domain.entity.Comments;
import com.qzxy.premiumhotelsystem.domain.vo.CommentVo;
import com.qzxy.premiumhotelsystem.service.CommentsService;
import com.qzxy.premiumhotelsystem.mapper.CommentsMapper;
import com.qzxy.premiumhotelsystem.service.LogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 14912
 * @description 针对表【comments】的数据库操作Service实现
 * @createDate 2024-09-12 15:55:14
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments>
        implements CommentsService {
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private LogsService logsService;
//    private LogsService logsService;

    @Override
    public List<CommentVo> searchAllByRoomId(String roomId) {
        return commentsMapper.searchAllByRoomId(roomId);
    }

    @Override
    public String create(Comments comments) {
        try {
            boolean b = commentsMapper.create(comments);
//            logsService.create("评论创建成功");
            return "评论创建成功";
        } catch (Exception e) {
//            logsService.create("评论创建失败");
            throw new RuntimeException("有些id不存在");
        }
    }

    /**
     * 根据房间号查询评论
     *
     * @param room_number
     * @return
     */
    @Override
    public List<String> searchAllCommentsByRoomId(String room_number) {
        return commentsMapper.searchAllCommentsByRoomId(room_number);
    }

    /**
     * 根据房间号查询一级评论
     * @param room_number
     * @return
     */
    @Override
    public List<CommentVo> searchAllByRoomIdAndParentId(String room_number) {
        return commentsMapper.searchAllByRoomIdAndParentId(room_number);
    }



    /**
     * 查询所有评论
     *
     * @param parentId
     * @return
     */
    @Override
    public List<CommentVo> getCommentsHierarchy(String parentId) {
        List<CommentVo> allComments = commentsMapper.selectAllComments();
        System.out.println("allComments::" + allComments);
        Map<Long, List<CommentVo>> commentsMap = allComments.stream()
                .filter(comment -> comment.getParentId() != null) // 过滤掉 parentId 为 null 的评论
                .collect(Collectors.groupingBy(CommentVo::getParentId));

        System.out.println("commentsMap::" + commentsMap);
        return buildHierarchy(commentsMap, Long.valueOf(parentId));
    }

    @Override
    public List<CommentVo> selectAllComments() {
        return commentsMapper.selectAllComments();
    }

    private List<CommentVo> buildHierarchy(Map<Long, List<CommentVo>> commentsMap, Long parentId) {
        List<CommentVo> comments = commentsMap.getOrDefault(parentId, new ArrayList<>());

        for (CommentVo comment : comments) {
            comment.setReplies(buildHierarchy(commentsMap, comment.getCommentId()));
        }
        return comments;
    }


}




